/*
Copyright To change this template, choose Tools | Templates
Copyright and open the template in the editor.
 */
package com.click.app.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * @author Joel Chuca Condo
 * @date 07/04/2009
 */
@Entity
@Table(name="purchases")
public class Purchase extends BaseObject {

    private Long id;
    private Bill bill;
    private Coin coin;
    private Supplier supplier;
    private User user;
    private Date date;
    private Date timeCredit;
    private Float totalPrice;
    private Float restPrice;
    private Boolean state;
    private Float discount;
    private List<PurchasePayment> purchasePayments = new ArrayList<PurchasePayment>();
    private List<PurchaseDetail> purchaseDetails = new ArrayList<PurchaseDetail>();


    public Purchase(){
        date = new Date();
        totalPrice = new Float(0);
        restPrice = new Float(0);
        discount = new Float(0);
        state = true;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="id_purchase")    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="id_bill")
    public Bill getBill() {
        return bill;
    }

    public void setBill(Bill Bill) {
        this.bill = Bill;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_coin")
    public Coin getCoin() {
        return this.coin;
    }

    public void setCoin(Coin coin) {
        this.coin = coin;

    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_supplier", nullable = false)
    public Supplier getSupplier() {
        return this.supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_user", nullable = false)
    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "date", length = 10)
    public Date getDate() {
        return this.date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "time_credit", length = 10)
    public Date getTimeCredit() {
        return this.timeCredit;
    }

    public void setTimeCredit(Date timeCredit) {
        this.timeCredit = timeCredit;
    }

    @Column(name = "price_total", precision = 12, scale = 0, columnDefinition="real default 0")
    public Float getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Float totalPrice) {
        this.totalPrice = totalPrice;
    }

    @Column(name = "price_rest", precision = 12, scale = 0 )
    public Float getRestPrice() {
        return restPrice;
    }

    public void setRestPrice(Float restPrice) {
        this.restPrice = restPrice;
    }

    @Column(name = "state")
    public Boolean getState() {
        return this.state;
    }

    public void setState(Boolean state) {
        this.state = state;
    }

    @Column(name = "discount")
    public Float getDiscount() {
        return discount;
    }

    public void setDiscount(Float discount) {
        this.discount = discount;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchase")
    public List<PurchasePayment> getPurchasePayments() {
        return purchasePayments;
    }

    public void setPurchasePayments(List<PurchasePayment> purchasePayments) {
        this.purchasePayments = purchasePayments;
    }
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "purchase")
    public List<PurchaseDetail> getPurchaseDetails() {
        if (purchaseDetails==null){
            purchaseDetails = new ArrayList<PurchaseDetail>();
        }
        return this.purchaseDetails;
    }

    public void setPurchaseDetails(List<PurchaseDetail> purchaseDetailses) {
        this.purchaseDetails = purchaseDetailses;
    }

    @Transient
    public Float getRealPrice() {
        return totalPrice-discount;
    }


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Purchase)) {
            return false;
        }
        Purchase other = (Purchase) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.click.app.model.Purchase[id=" + id + "]";
    }

    @Transient
    public float getDetailsTotal(){
        float total = 0.0f;
        for (PurchaseDetail pd:getPurchaseDetails()){
            total +=(pd.getTotalPrice());
        }
        return total;
    }
}
